{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com\n",
      "Requirement already satisfied: torch==2.2 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (2.2.0)\n",
      "Requirement already satisfied: filelock in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torch==2.2) (3.13.1)\n",
      "Requirement already satisfied: typing-extensions>=4.8.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torch==2.2) (4.9.0)\n",
      "Requirement already satisfied: sympy in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torch==2.2) (1.12)\n",
      "Requirement already satisfied: networkx in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torch==2.2) (2.8.5)\n",
      "Requirement already satisfied: jinja2 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torch==2.2) (3.1.3)\n",
      "Requirement already satisfied: fsspec in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torch==2.2) (2024.2.0)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from jinja2->torch==2.2) (2.1.5)\n",
      "Requirement already satisfied: mpmath>=0.19 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from sympy->torch==2.2) (1.3.0)\n",
      "\u001b[33mDEPRECATION: pytorch-lightning 1.7.0 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0mLooking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com\n",
      "Requirement already satisfied: torchvision==0.17 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (0.17.0)\n",
      "Requirement already satisfied: numpy in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torchvision==0.17) (1.26.4)\n",
      "Requirement already satisfied: requests in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torchvision==0.17) (2.31.0)\n",
      "Requirement already satisfied: torch==2.2.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torchvision==0.17) (2.2.0)\n",
      "Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torchvision==0.17) (9.3.0)\n",
      "Requirement already satisfied: filelock in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torch==2.2.0->torchvision==0.17) (3.13.1)\n",
      "Requirement already satisfied: typing-extensions>=4.8.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torch==2.2.0->torchvision==0.17) (4.9.0)\n",
      "Requirement already satisfied: sympy in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torch==2.2.0->torchvision==0.17) (1.12)\n",
      "Requirement already satisfied: networkx in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torch==2.2.0->torchvision==0.17) (2.8.5)\n",
      "Requirement already satisfied: jinja2 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torch==2.2.0->torchvision==0.17) (3.1.3)\n",
      "Requirement already satisfied: fsspec in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from torch==2.2.0->torchvision==0.17) (2024.2.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from requests->torchvision==0.17) (3.3.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from requests->torchvision==0.17) (3.6)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from requests->torchvision==0.17) (2.2.1)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from requests->torchvision==0.17) (2024.2.2)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from jinja2->torch==2.2.0->torchvision==0.17) (2.1.5)\n",
      "Requirement already satisfied: mpmath>=0.19 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from sympy->torch==2.2.0->torchvision==0.17) (1.3.0)\n",
      "\u001b[33mDEPRECATION: pytorch-lightning 1.7.0 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0mLooking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com\n",
      "Requirement already satisfied: tensorflow==2.15.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (2.15.0)\n",
      "Requirement already satisfied: tensorflow-macos==2.15.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow==2.15.0) (2.15.0)\n",
      "Requirement already satisfied: absl-py>=1.0.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (2.1.0)\n",
      "Requirement already satisfied: astunparse>=1.6.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (1.6.3)\n",
      "Requirement already satisfied: flatbuffers>=23.5.26 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (23.5.26)\n",
      "Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (0.5.4)\n",
      "Requirement already satisfied: google-pasta>=0.1.1 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (0.2.0)\n",
      "Requirement already satisfied: h5py>=2.9.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (3.10.0)\n",
      "Requirement already satisfied: libclang>=13.0.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (16.0.6)\n",
      "Requirement already satisfied: ml-dtypes~=0.2.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (0.2.0)\n",
      "Requirement already satisfied: numpy<2.0.0,>=1.23.5 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (1.26.4)\n",
      "Requirement already satisfied: opt-einsum>=2.3.2 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (3.3.0)\n",
      "Requirement already satisfied: packaging in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (23.2)\n",
      "Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (3.20.3)\n",
      "Requirement already satisfied: setuptools in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (68.2.2)\n",
      "Requirement already satisfied: six>=1.12.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (1.16.0)\n",
      "Requirement already satisfied: termcolor>=1.1.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (2.4.0)\n",
      "Requirement already satisfied: typing-extensions>=3.6.6 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (4.9.0)\n",
      "Requirement already satisfied: wrapt<1.15,>=1.11.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (1.14.1)\n",
      "Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (0.36.0)\n",
      "Requirement already satisfied: grpcio<2.0,>=1.24.3 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (1.60.1)\n",
      "Requirement already satisfied: tensorboard<2.16,>=2.15 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (2.15.2)\n",
      "Requirement already satisfied: tensorflow-estimator<2.16,>=2.15.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (2.15.0)\n",
      "Requirement already satisfied: keras<2.16,>=2.15.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorflow-macos==2.15.0->tensorflow==2.15.0) (2.15.0)\n",
      "Requirement already satisfied: wheel<1.0,>=0.23.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from astunparse>=1.6.0->tensorflow-macos==2.15.0->tensorflow==2.15.0) (0.41.2)\n",
      "Requirement already satisfied: google-auth<3,>=1.6.3 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (2.28.0)\n",
      "Requirement already satisfied: google-auth-oauthlib<2,>=0.5 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (1.2.0)\n",
      "Requirement already satisfied: markdown>=2.6.8 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (3.5.2)\n",
      "Requirement already satisfied: requests<3,>=2.21.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (2.31.0)\n",
      "Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (0.7.2)\n",
      "Requirement already satisfied: werkzeug>=1.0.1 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (3.0.1)\n",
      "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (5.3.2)\n",
      "Requirement already satisfied: pyasn1-modules>=0.2.1 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (0.3.0)\n",
      "Requirement already satisfied: rsa<5,>=3.1.4 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (4.9)\n",
      "Requirement already satisfied: requests-oauthlib>=0.7.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from google-auth-oauthlib<2,>=0.5->tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (1.3.1)\n",
      "Requirement already satisfied: importlib-metadata>=4.4 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from markdown>=2.6.8->tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (7.0.1)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (3.3.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (3.6)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (2.2.1)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (2024.2.2)\n",
      "Requirement already satisfied: MarkupSafe>=2.1.1 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from werkzeug>=1.0.1->tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (2.1.5)\n",
      "Requirement already satisfied: zipp>=0.5 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (3.17.0)\n",
      "Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (0.5.1)\n",
      "Requirement already satisfied: oauthlib>=3.0.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<2,>=0.5->tensorboard<2.16,>=2.15->tensorflow-macos==2.15.0->tensorflow==2.15.0) (3.2.2)\n",
      "\u001b[33mDEPRECATION: pytorch-lightning 1.7.0 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0mLooking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com\n",
      "Requirement already satisfied: onnx2tf==1.19.11 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (1.19.11)\n",
      "\u001b[33mDEPRECATION: pytorch-lightning 1.7.0 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0mLooking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com\n",
      "Requirement already satisfied: onnx==1.15.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (1.15.0)\n",
      "Requirement already satisfied: numpy in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from onnx==1.15.0) (1.26.4)\n",
      "Requirement already satisfied: protobuf>=3.20.2 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from onnx==1.15.0) (3.20.3)\n",
      "\u001b[33mDEPRECATION: pytorch-lightning 1.7.0 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0mLooking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com\n",
      "Requirement already satisfied: Pillow==9.3.0 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (9.3.0)\n",
      "\u001b[33mDEPRECATION: pytorch-lightning 1.7.0 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0mLooking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com\n",
      "Requirement already satisfied: nvidia-pyindex==1.0.9 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (1.0.9)\n",
      "\u001b[33mDEPRECATION: pytorch-lightning 1.7.0 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0mLooking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com\n",
      "Requirement already satisfied: onnx-graphsurgeon==0.3.27 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (0.3.27)\n",
      "Requirement already satisfied: numpy in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from onnx-graphsurgeon==0.3.27) (1.26.4)\n",
      "Requirement already satisfied: onnx in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from onnx-graphsurgeon==0.3.27) (1.15.0)\n",
      "Requirement already satisfied: protobuf>=3.20.2 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (from onnx->onnx-graphsurgeon==0.3.27) (3.20.3)\n",
      "\u001b[33mDEPRECATION: pytorch-lightning 1.7.0 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0mLooking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com\n",
      "Requirement already satisfied: sng4onnx==1.0.1 in /Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages (1.0.1)\n",
      "\u001b[33mDEPRECATION: pytorch-lightning 1.7.0 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "!pip install torch==2.2\n",
    "!pip install torchvision==0.17\n",
    "!pip install tensorflow==2.15.0\n",
    "!pip install onnx2tf==1.19.11\n",
    "!pip install onnx==1.15.0\n",
    "!pip install Pillow==9.3.0\n",
    "!pip install nvidia-pyindex==1.0.9\n",
    "!pip install onnx-graphsurgeon==0.3.27\n",
    "!pip install sng4onnx==1.0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "import torch.optim as optim\n",
    "from torch.utils.data import DataLoader\n",
    "from torchvision import datasets, transforms\n",
    "\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "\n",
    "import tensorflow as tf\n",
    "import onnx2tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ConvNet(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(ConvNet, self).__init__()\n",
    "        self.cn1 = nn.Conv2d(1, 16, 3, 1)\n",
    "        self.cn2 = nn.Conv2d(16, 32, 3, 1)\n",
    "        self.dp1 = nn.Dropout2d(0.10)\n",
    "        self.dp2 = nn.Dropout2d(0.25)\n",
    "        self.fc1 = nn.Linear(4608, 64) # 4608 is basically 12 X 12 X 32\n",
    "        self.fc2 = nn.Linear(64, 10)\n",
    " \n",
    "    def forward(self, x):\n",
    "        x = self.cn1(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.cn2(x)\n",
    "        x = F.relu(x)\n",
    "        x = F.max_pool2d(x, 2)\n",
    "        x = self.dp1(x)\n",
    "        x = torch.flatten(x, 1)\n",
    "        x = self.fc1(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.dp2(x)\n",
    "        x = self.fc2(x)\n",
    "        op = F.log_softmax(x, dim=1)\n",
    "        return op\n",
    "    \n",
    "model = ConvNet()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<All keys matched successfully>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PATH_TO_MODEL = \"./convnet.pth\"\n",
    "model.load_state_dict(torch.load(PATH_TO_MODEL, map_location=\"cpu\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = Image.open(\"./digit_image.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def image_to_tensor(image):\n",
    "    gray_image = transforms.functional.to_grayscale(image)\n",
    "    resized_image = transforms.functional.resize(gray_image, (28, 28))\n",
    "    input_image_tensor = transforms.functional.to_tensor(resized_image)\n",
    "    input_image_tensor_norm = transforms.functional.normalize(input_image_tensor, (0.1302,), (0.3069,))\n",
    "    return input_image_tensor_norm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_tensor = image_to_tensor(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.eval()\n",
    "for p in model.parameters():\n",
    "    p.requires_grad_(False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/Ashish.Jha/anaconda3/envs/python39/lib/python3.9/site-packages/torch/nn/functional.py:1347: UserWarning: dropout2d: Received a 2-D input to dropout2d, which is deprecated and will result in an error in a future release. To retain the behavior and silence this warning, please use dropout instead. Note that dropout2d exists to provide channel-wise dropout on inputs with 2 spatial dimensions, a channel dimension, and an optional batch dimension (i.e. 3D or 4D inputs).\n",
      "  warnings.warn(warn_msg)\n"
     ]
    }
   ],
   "source": [
    "demo_input = torch.ones(1, 1, 28, 28)\n",
    "torch.onnx.export(model, demo_input, \"convnet.onnx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Summary on the non-converted ops:\n",
      "---------------------------------\n",
      " * Accepted dialects: tfl, builtin, func\n",
      " * Non-Converted Ops: 10, Total Ops 21, % non-converted = 47.62 %\n",
      " * 10 ARITH ops\n",
      "\n",
      "- arith.constant:   10 occurrences  (f32: 8, i32: 2)\n",
      "\n",
      "\n",
      "\n",
      "  (f32: 2)\n",
      "  (f32: 2)\n",
      "  (f32: 1)\n",
      "  (f32: 1)\n",
      "  (f32: 1)\n",
      "  (f32: 1)\n",
      "Summary on the non-converted ops:\n",
      "---------------------------------\n",
      " * Accepted dialects: tfl, builtin, func\n",
      " * Non-Converted Ops: 10, Total Ops 29, % non-converted = 34.48 %\n",
      " * 10 ARITH ops\n",
      "\n",
      "- arith.constant:   10 occurrences  (f16: 8, i32: 2)\n",
      "\n",
      "\n",
      "\n",
      "  (f32: 2)\n",
      "  (f32: 8)\n",
      "  (f32: 2)\n",
      "  (f32: 1)\n",
      "  (f32: 1)\n",
      "  (f32: 1)\n",
      "  (f32: 1)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.src.engine.functional.Functional at 0x2f6c5d460>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Now, let's convert the ONNX model to TF\n",
    "onnx2tf.convert(\n",
    "    input_onnx_file_path=\"convnet.onnx\",\n",
    "    output_folder_path=\"convnet_tf\",\n",
    "    non_verbose=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<ConcreteFunction (inputs_0: TensorSpec(shape=(1, 28, 28, 1), dtype=tf.float32, name='inputs_0')) -> TensorSpec(shape=(1, 10), dtype=tf.float32, name='unknown') at 0x2F71EEBB0>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Load the saved model\n",
    "model = tf.saved_model.load(\"./convnet_tf/\")\n",
    "\n",
    "model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model Output: tf.Tensor(\n",
      "[[-1.04575987e+01 -1.39286604e+01 -2.57339468e-03 -8.81325722e+00\n",
      "  -1.02665386e+01 -1.58330278e+01 -1.25931921e+01 -1.39404545e+01\n",
      "  -6.05328608e+00 -1.29597530e+01]], shape=(1, 10), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "# Perform inference\n",
    "output = model(input_tensor.unsqueeze(-1))\n",
    "\n",
    "# Print the output\n",
    "print(\"Model Output:\", output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
